package com.example.gomoku;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.gomoku.DBHelper;
import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText et_rid;
    private EditText et_rpwd;
    private EditText et_rname;
    private SharedPreferences prefs;
    private SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_register );

        initView();
        prefs = getSharedPreferences("user", Context.MODE_PRIVATE );
        editor = prefs.edit();

    }

     void initView() {
        et_rid = findViewById( R.id.et_rsid );
        et_rname = findViewById( R.id.et_rname );
        et_rpwd = findViewById( R.id.et_rpwd );
        findViewById( R.id.btn_registeruser ).setOnClickListener( this );

    }

    @Override
    public void onClick(View v) {
        String uid = et_rid.getText().toString();
        String upwd = et_rpwd.getText().toString();
        String uname = et_rname.getText().toString();
        DBHelper dbHelper = new DBHelper( this );
        if (uid.isEmpty() || upwd.isEmpty() || uname.isEmpty()) {
            Toast.makeText(this, "请填写所有字段", Toast.LENGTH_SHORT).show();
            return;
        }
        User user = new User(uid,upwd,uname);
        long result= dbHelper.registerUser( user );
        if (result != -1) {
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
        }
    }
}